//
//  WBTabBar.m
//  sinaWeibo
//
//  Created by 李江波 on 2016/11/14.
//  Copyright © 2016年 lijiangbo. All rights reserved.
//

#import "WBTabBar.h"



@interface WBTabBar ()

@property(nonatomic ,strong) UIButton *btn;

@end

@implementation WBTabBar

-(instancetype)init{

    self = [super init];
    if (self) {
        
        _btn = [UIButton buttonWithType:0];
      
        NSAttributedString *str = [NSAttributedString cz_imageTextWithImage:[UIImage imageNamed:@"shopCart"] imageWH:25 title:@"购物车" fontSize:10 titleColor:[UIColor  grayColor] spacing:3];
//        NSAttributedString *str1 = [NSAttributedString cz_imageTextWithImage:[UIImage imageNamed:@"shopCart_r"] imageWH:25 title:@"购物车" fontSize:10 titleColor:[UIColor grayColor] spacing:3];
        
        [_btn setAttributedTitle:str forState:UIControlStateNormal];
//        [_btn setAttributedTitle:str1 forState:UIControlStateHighlighted];
//        
        [_btn sizeToFit];
        _btn.titleLabel.numberOfLines = 0;
        _btn.titleLabel.textAlignment = NSTextAlignmentCenter;
        
        [_btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
        
        [_btn.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
           
            make.centerX.equalTo(_btn);
            make.top.equalTo(_btn).offset(6);
            make.bottom.equalTo(_btn);
        }];
        
        [self addSubview:_btn];
        
    }
    return self;
}

-(void)btnClick{

    if (_btnClickBlock != nil) {
        
        _btnClickBlock();
    }
    
}


-(void)layoutSubviews{

    [super layoutSubviews];
    
//    NSLog(@"%@",self.subviews);
    
    CGFloat itemW = self.bounds.size.width / 4;
    CGFloat itemH = self.bounds.size.height;
    __block NSInteger index = 0;
    
    [self.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        if ([obj isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            
            obj.frame = CGRectMake(index * itemW, 0, itemW, itemH);
            
            index += (index == 1) ? 2 : 1 ;
            
        }
        
    }];
    
    _btn.bounds = CGRectMake(0, 0, itemW, itemH);
    _btn.center = CGPointMake(self.bounds.size.width * 0.5 + itemW * 0.5, self.bounds.size.height * 0.5);
    
    
    
}














@end
